package com.teichmann.core.targets.online;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;

import org.apache.commons.lang.StringUtils;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.lexer.Lexer;
import org.htmlparser.util.ParserException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.teichmann.bo.Car;
import com.teichmann.bo.Seller;
import com.teichmann.core.OnlinePageJSoup;
import com.teichmann.core.annotations.Url;
import com.teichmann.core.util.ConnectionUtils;
import com.teichmann.core.util.CrawlerUtils;

@Url(value = "http://www.sulrevendas.com.br/busca_veiculo.asp")
public class SulRevendasJSoup implements OnlinePageJSoup {

	ArrayList<Car> cars = new ArrayList<Car>();

	public ArrayList<Car> performSearch(String parameter) throws IOException {

		Map<String, String> parameters = new HashMap<String, String>();
		parameters.put("mo", parameter);

		String responseBody = ConnectionUtils.post(this.getClass().getAnnotation(Url.class)
				.value(), parameters);

		int numberOfPages = org.apache.commons.lang.StringUtils.countMatches(responseBody,
				"pq=&pag=");

		numberOfPages = (numberOfPages % 2 == 0) ? (numberOfPages / 2) + 1
				: numberOfPages++;

		for (int x = 1; x <= numberOfPages; x++) {
			responseBody = ConnectionUtils
					.get("http://www.sulrevendas.com.br/busca_veiculo.asp?mo=" + parameter
							+ "&pag=" + x);

			Document doc = Jsoup.parseBodyFragment(responseBody);
			Elements elements = doc.getElementsByAttributeValueContaining("onmouseover",
					"this.style.backgroundColor = '#E0E0E0';");

			for (Element e : elements) {
				cars.add(extractInformation(e));
			}
		}

		return cars;

	}

	public Car extractInformation(Element line) {
		Car car = new Car();
		try {

			car.setModelo(CrawlerUtils.removeUnusedFields(line.child(0).childNode(1)
					.childNode(3).childNode(2).toString()));
			car.setMontadora(CrawlerUtils.removeUnusedFields(line.child(0).childNode(1)
					.childNode(3).childNode(0).childNode(0).toString()));
			car.setCor(CrawlerUtils.removeUnusedFields(line.child(0).childNode(1).childNode(
					7).childNode(2).toString()));
			car.setCombustivel(CrawlerUtils.removeUnusedFields(line.child(0).childNode(1)
					.childNode(9).childNode(2).toString()));
			car.setValor(CrawlerUtils.removeUnusedFields(line.child(0).childNode(3)
					.childNode(7).childNode(0).toString()));
			car.setUrl("http://www.sulrevendas.com.br/"
					+ line.child(0).childNode(1).childNode(13).childNode(0).attr("href"));

			car.setAnoFabricacao(new Integer((line.child(0).childNode(1).childNode(5)
					.childNode(2).toString()).split("/")[0]));
			car.setAnoFabricacao(new Integer((line.child(0).childNode(1).childNode(5)
					.childNode(2).toString()).split("/")[1]));

		} catch (Exception e) {
			// e.printStackTrace();
		}

		return car;

	}

	public Car extractInformation(Tag line) throws IOException {
		// TODO Auto-generated method stub
		return null;
	}

}
